part of main;

class Delivery{
  bool legal;
  int over;
  int ball;
  int runs; // null means illegal delivery, for now
  Player bowler;
  Player batsman;
  String pre;
  String comm;
  String post;
  
  // get rid of these eventually
  String event;
  String dismissal;

  static var _specialWords = ['<b>', 'century', 'fifty', 'half century', 
                              'sensational', 'fantastic', 'brilliant', 
                              'wonderful', 'dropped', 'drop']; 
  
  bool get isSpecial{
    if (runs == 4 || runs == 6) return true;
    if (!dismissal.isEmpty) return true;
    for (var each in _specialWords) if (comm.contains(each)) return true;
    return false;
  }
  
  bool get isComplete => !comm.isEmpty;
  
  Delivery(Map<String, String> data){
    if (data.isEmpty) throw const FormatException("empty map");
    if (!data.containsKey('overs_actual')) throw const FormatException('not a delivery');
    
    if (data['event'].contains('no ball') || 
        data['event'].contains('wide')) legal = false;
    else legal = true;
    
    var over_plus_ball = data['overs_actual'].split('.');
    over = int.parse(over_plus_ball[0]);
    ball = int.parse(over_plus_ball[1]);
    
    switch (data['event']){
      case 'no run' : runs = 0; break;
      case '1 run' : runs = 1; break;
      case '2 runs' : runs = 2; break;
      case '3 runs' : runs = 3; break;
      case 'FOUR' : runs = 4; break;
      case 'SIX' : runs = 6; break;
      case 'OUT' : runs = 0; break;
    }
    
    var bat_bowl = data['players'].split(' to ');
    bowler = new Player(bat_bowl[0]);
    batsman = new Player(bat_bowl[1]);
    
    if (data.containsKey('pre_text')) pre = data['pre_text'];
    comm = data['text'];
    if (data.containsKey('post_text')) post = data['post_text'];
    
    // get rid of these eventually
    event = data['event'];
    dismissal = data['dismissal'];
  }
  
  update(Map<String, String> data){
    if (data.containsKey('pre_text')) pre = data['pre_text'];
    comm = data['text'];
    if (data.containsKey('post_text')) post = data['post_text'];
  }
  
  String toString() => '$over.$ball, $bowler to $batsman, $event, $comm, $dismissal';
}

class Player{
  final String name;
  Player(this.name);
  String toString() => name;
}

void testing(){
  var data = {
              "comms_id": "1537617",
              "dismissal": "Marshall Ayub c Silva b Lakmal 18 (36b 3x4 0x6) SR: 50.00",
              "event": "OUT",
              "innings_number": "3",
              "over_number": "15",
              "overs_actual": "14.5",
              "overs_unique": "14.06",
              "players": "Lakmal to Marshall Ayub",
              "pre_text": "<p>\nTwo slips, a leg gully and short leg\n<\/p>",
              "speed_kph": "",
              "speed_mph": "",
              "text": "the short ball does the trick! Lakmal comes round the wicket and bangs it in short on the body, gives Ayub little option but to play to it, he tries to fend it off off the back foot and the ball lobs to Silva waiting at short leg, easy catch"
            };
  Delivery test = new Delivery(data);
  print(test);
}
